/**
 * Created by maiseries on 2017/9/25.
 */
import React from 'react';
import {StackNavigator, TabNavigator, NavigationActions} from 'react-navigation';
import CardStackStyleInterpolator from 'react-navigation/src/views/CardStack/CardStackStyleInterpolator'

import TabBarMidModal from './components/TabBarMidModal'

/**
 * Global
 */
import Splash from './views/Splash';
import Guide from './views/Guide';
/**
 * TabNavigator
 */
import Index from './views/Index';
import Release from './views/Release';
import Mine from './views/Mine';
/**
 * Document
 */
import BusinessForm from './views/document/BusinessForm'
import AboutUs from './views/document/AboutUs'
import Agreement from './views/document/Agreement'
import AdWebView from './views/document/AdWebView'
import AssetSummaryList from './views/document/AssetSummaryList'
import AssetSummary from './views/document/AssetSummary'
import DataSupport from './views/document/DataSupport'
import LawList from './views/document/LawList'
import HelpList from './views/document/HelpList'
import AboutMai from './views/document/AboutMai'
import Help from './views/document/Help'
import SubjectDetail from './views/document/SubjectDetail'
/**
 * mine
 */
import MessageList from './views/mine/MessageList'
import SystemMessage from './views/mine/SystemMessage'
import Message from './views/mine/Message'
import Feedback from './views/mine/Feedback'
import Setting from './views/mine/Setting'
import Modify from './views/mine/Modify'
/**
 * passport
 */
import Login from './views/passport/Login'
import Forgot from './views/passport/Forgot'
/**
 * auth
 */
import AuthList from './views/auth/AuthList'
import AuthApprove from './views/auth/AuthApprove'
import AuthForm from './views/auth/AuthForm'
import AuthProForm from './views/auth/AuthProForm'
import AuthImage from './views/auth/AuthImage'
/**
 * demand
 */
import DemandForm from './views/demand/DemandForm'
import DemandDetail from './views/demand/DemandDetail'
import DemandList from './views/demand/DemandList'
import AssetTypeList from './views/demand/AssetTypeList'
import AssetTypeChildrenList from './views/demand/AssetTypeChildrenList'
/**
 * asset
 */
import AssetList from './views/asset/AssetList'
import AssetDetail from './views/asset/AssetDetail'

/** Tab路由 **/
const tabRoutes = {
    Index, Release, Mine
};

const tabOptions = {
    tabBarPosition: 'bottom',
    swipeEnabled: false,
    animationEnabled: false,
    lazyLoad: false,
    backBehavior: 'none',
    tabBarOptions: {
        bgSource: require('./sources/tabbar_bg.png'),
        openModal(self) {
            return (
                <TabBarMidModal self={self}/>
            )
        },
        tintColor: '#333',
        inactiveTintColor: '#333',
        activeTintColor: layout.MAIN_COLOR,
        tabStyle: {
            backgroundColor: 'transparent'
        },
        showIcon: true,
        style: {
            position: 'absolute',
            bottom: 0,
            left: 0,
            width: layout.MAX_WIDTH,
            height: layout.BOTTOM_TAB_HEIGHT,
            backgroundColor: 'transparent',
        },
        labelStyle: {
            fontSize: layout.font(20),
            fontWeight: '100'
        },
    },
    initialRouteName: Index.name
};

const Tab = TabNavigator(tabRoutes, tabOptions);

/** Stack路由 **/
const stackRoutes = {
    Tab: {
        screen: Tab,
    },
    Index: {
        screen: Tab,
        path: Index.path,
        sign: Index.sign
    },
    Mine: {
        screen: Tab,
        path: Mine.path,
        sign: Mine.sign
    },
    Splash, Guide,
    BusinessForm, Login, Agreement, AboutUs, AdWebView, AssetSummaryList, AssetSummary, Forgot, DataSupport, LawList, HelpList,
    AboutMai, Help, SubjectDetail,                               //sign = false
    MessageList, Feedback, Setting, Modify, SystemMessage, Message,                                                               //sign = true
    AuthList, AuthApprove, AuthForm, AuthProForm, AuthImage,                                 //sign = true
    DemandForm, DemandDetail, AssetTypeList, AssetTypeChildrenList, DemandList,               //sign = true
    AssetList, AssetDetail                                              //sign = true
};

const stackOptions = {
    initialRouteName: Splash.name,
    transitionConfig: () => ({
        screenInterpolator: CardStackStyleInterpolator.forHorizontal
    }),
    navigationOptions: {
        gesturesEnabled: false,
    }
};

const Stack = StackNavigator(stackRoutes, stackOptions);

const defaultStateForAction = Stack.router.getStateForAction;

Stack.router.getStateForAction = (action, state) => {
    let todoAction = action;
    //处理登录拦截
    if (action
        && action.routeName
        && stackRoutes[action.routeName]
        && stackRoutes[action.routeName].sign
        && !user.loginState) {
        todoAction = NavigationActions.navigate({routeName: Login.name});
    }
    //防止多次打开
    // if (state && todoAction.routeName === state.routes[state.routes.length - 1].routeName) {
    //     return;
    // }
    if(todoAction.type === 'Navigation/NAVIGATE') {
        todoAction.params = {...todoAction.params, prevState : state.routes}
    }
    return defaultStateForAction(todoAction, state);
};

export default class MainApp extends React.Component {
    render() {
        return (
            <Stack/>
        )
    }
}